<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 180px;
            height: 30px;
            margin: 100px 200px;
            background-color: #fff;
        }

        div {
            float: left;
            width: 50px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            margin-right: 2px;
            background-color: #666;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="hour">1</div>
        <div class="minute">2</div>
        <div class="second">3</div>
    </div>
    <script>
        //setTimeout() 该定时器在定时器到期后执行调用函数
        //setInerval() 重复调用一个函数，每隔这个时间，就去调用一次回调函数

        /*  区别: setTimeout 延迟时间到了，就去调用回调函数，只调用一次 就结束了这个定时器
                  setInerval 每隔这个延迟时间 就去调用这个回调函数，会调用很多次，重复调用这个定时器
        */

      var hour = document.querySelector('.hour');
      var minute = document.querySelector('.minute');
      var second = document.querySelector('.second');
      var inputTime = +new Date('2021-11-9 21:00:00')

      getDown(); //先调用这个函数，以防刷新有空白
     
      setInterval(getDown, 1000);
    
      function getDown() {
          var getTime = +new Date();
          var times = (inputTime - getTime) / 1000  //剩余秒数
          var h = parseInt(times / 60 / 60 % 24) //剩余小时
          h = h < 10 ? '0' + h : h;
          hour.innerHTML = h;
          var m = parseInt(times / 60 % 60) 
          m = m < 10 ? '0' + m :m;
          minute.innerHTML = m;
          s = parseInt(times % 60) 
          s = s < 10 ? '0' + s : s;
          second.innerHTML = s;
      }
    </script>
</body>

</html>